package com.inspur.icity.bus.restapi;

import com.inspur.icity.bus.api.user.enumeration.AreaCodeEnum;
import com.inspur.icity.bus.api.user.model.BusRoute;
import com.inspur.icity.bus.api.user.service.BusRouteService;
import com.inspur.icity.bus.base.core.model.BaseResult;
import com.inspur.icity.bus.base.core.web.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 济南城际公交Controller
 */
@Controller
@RequestMapping(value = "/jn")
public class BusJiNanHomeController extends BaseController {

    @Autowired
    private BusRouteService busRouteService;

    /**
     * 获取城际公交数据
     * @return
     */
    @RequestMapping(value = "/index",method = RequestMethod.GET)
    @ResponseBody
    public Object index(){
        Map<String,Object> resultMap = new HashMap<>();
        //获取路线列表
        Map<String,Object> searchMap = new HashMap<>();
        String areacode = AreaCodeEnum.getAreaCode("济南市");
        searchMap.put("areacode",areacode);
        searchMap.put("updown",1); //上行
        List<BusRoute> list = busRouteService.find(searchMap);
        List<Map<String,Object>> resultList = new ArrayList<>();
        Map<String,Object> map = null;
        for (BusRoute busRoute : list){
            map = new HashMap<>();
            map.put("name",busRoute.getRouteName());
            map.put("beginStationName",busRoute.getBeginStationName());
            map.put("endStationName",busRoute.getEndStationName());
            map.put("direction",busRoute.getDirection());
            map.put("areacode",areacode);
            map.put("firstOrder",busRoute.getFirstOrder());
            map.put("lastOrder",busRoute.getLastOrder());
            map.put("ticket",busRoute.getTicket());

            resultList.add(map);
        }
        resultMap.put("routeData", resultList);
        resultMap.put("dataSource", "山东省交运集团");
        return BaseResult.success(resultMap);
    }

    /**
     * 获取线路详情
     * @param areacode 系统代号
     * @param routeName 线路名称
     * @param direction 线路方向
     * @return
     */
    @RequestMapping(value = "/getRouteDetailsForJn",method = RequestMethod.GET)
    @ResponseBody
    public Object getRouteDetails(@RequestParam(value = "areacode", required = true)String areacode,
                                  @RequestParam(value = "routeName", required = true)String routeName,
                                  @RequestParam(value = "direction", required = true)String direction,
                                  @RequestParam(value = "flag", required = false)String flag,
                                  @RequestParam(value = "mobile", required = false)String mobile,
                                  @RequestParam(value = "longitude", required = false)Double longitude,
                                  @RequestParam(value = "latitude", required = false)Double latitude,
                                  @RequestParam(value = "isTnterval", required = true)Boolean isTnterval){
        if(!direction.endsWith("方向")){
            direction = direction+"方向";
        }
        Map<String,Object> map = busRouteService.getRouteDetailsForJn(areacode,routeName,direction,flag,mobile,longitude,latitude,"no",isTnterval);
        return BaseResult.success(map);
    }

}